package org.example.java8time;

import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Classname LocalTimeTest
 * @Description TODO
 * @Date 2021/2/4 11:11 上午
 * @Created by xiangjd
 */
public class LocalTimeTest {


    @Test
    public void test2(){
        //获取当前时间
        LocalTime now = LocalTime.now();
        System.out.println(now);
        //根据参数设定当前时间
        LocalTime localTime = LocalTime.of(12,35,59);
        System.out.println(localTime);
        localTime = LocalTime.of(12,35);
        System.out.println(localTime);
    }


    /**
     * 获取当前时间的小时数
     * 获取当前时间的分钟数
     * 获取当前时间的秒数
     */
    @Test
    public  void test3(){
        LocalTime localTime = LocalTime.of(12,35,59);
        System.out.println(localTime.getHour());
        System.out.println(localTime.getMinute());
        System.out.println(localTime.getSecond());
    }

    /**
     * with开头的方法，我的理解是将参数替换localTime中的对应属性，
     * 重新计算得到新的时间。
     * 将参数中的"小时"替换localTime中的"小时"
     * 将参数中的"分钟"替换localTime中的"分钟"
     * 将参数中的"秒"替换localTime中的"秒"
     *
     * 将当前时间减一小时
     * 将当前时间减一分钟
     * 将当前时间减一秒
     * 将当前时间加一小时
     * 将当前时间加一分钟
     * 将当前时间加一秒
     */
    @Test
    public void test4(){
        LocalTime localTime = LocalTime.of(12,35,00);
        System.out.println(localTime.withHour(1));
        System.out.println(localTime.withMinute(1));
        System.out.println(localTime.withSecond(1));

        System.out.println(localTime.minusHours(1));
        System.out.println(localTime.minusMinutes(1));
        System.out.println(localTime.minusSeconds(10));
        System.out.println(localTime.plusHours(1));
        System.out.println(localTime.plusMinutes(1));
        System.out.println(localTime.plusSeconds(10));
    }


    /**
     * LocalTime转String
     * String转LocalTime
     * date转localTime
     * localTime转Date
     */
    @Test
    public void test8(){
        String format = LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(format);

        LocalTime parse = LocalTime.parse("14:32:24", DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(parse);

        LocalTime localTime = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
        System.out.println(localTime);

        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        //如果要修改localTime,要这样写不然下一步不能获取最新的值.
//        time = time.plusMinutes(30);
        LocalDateTime dateTimeFromDateAndTime = LocalDateTime.of(date, time);
        Date from = Date.from(dateTimeFromDateAndTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println(from);

    }
}
